package com.qianli.user.facade.impl;

import com.fqgj.common.api.Response;
import com.qianli.user.application.UserAccessApplication;
import com.qianli.user.facade.UserServiceFacade;
import com.qianli.user.ro.UserStateCode;
import com.qianli.user.ro.account.UserAccessRO;
import com.qianli.user.ro.account.UserLoginRO;
import com.qianli.user.ro.account.UserThirdPartRO;
import com.robert.vesta.util.IdUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * name:用户服务
 * auth:ting.li@qianli-inc.com
 * date:2017/12/28 10:34
 * version: v1.0.0
 **/
@Service("userServiceFacade")
public class UserServiceFacadeImpl implements UserServiceFacade {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceFacade.class);

    @Autowired
    private UserAccessApplication userAccessApplication;


    /**
     * 注册
     *
     * @param userLogin
     * @return
     */
    @Override
    public Response<UserAccessRO> register(UserLoginRO userLogin) {
        if (null == userLogin) {
            LOGGER.info("userServiceFacade.register param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "参数不能为空");
        }
        if (null == userLogin.getBiz() || null == userLogin.getAppCode()) {
            LOGGER.info("userServiceFacade.register param biz and appCode can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "参数biz and appCode不能为空");
        }
        if (null == userLogin.getMobile()) {
            LOGGER.info("userServiceFacade.register param mobile can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "参数mobile不能为空");
        }
        if (null == userLogin.getAppInfo()) {
            LOGGER.info("userServiceFacade.register param appInfo can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "参数appInfo不能为空");
        }
        Response<UserAccessRO> result = userAccessApplication.register(userLogin);
        return result;
    }

    /**
     * 登录
     *
     * @param userLogin
     * @return
     */
    @Override
    public Response<UserAccessRO> login(UserLoginRO userLogin) {
        if (null == userLogin) {
            LOGGER.info("userServiceFacade.register param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "参数不能为空");
        }
        if (null == userLogin.getBiz() || null == userLogin.getAppCode()) {
            LOGGER.info("userServiceFacade.register param biz and appCode can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "参数biz and appCode不能为空");
        }
        if (null == userLogin.getMobile()) {
            LOGGER.info("userServiceFacade.register param mobile can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "参数mobile不能为空");
        }
        if (null == userLogin.getAppInfo()) {
            LOGGER.info("userServiceFacade.register param appInfo can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "参数appInfo不能为空");
        }
        Response<UserAccessRO> result = userAccessApplication.login(userLogin);
        return result;
    }

    @Override
    public Response<String> produceUserCodeByMobile(String mobile) {
        if (null == mobile || mobile.length() != 11) {
            return Response.error();
        }
        String userCode = IdUtils.genId()+"";
        return Response.ok().putData(userCode);
    }

    @Override
    public Response<UserAccessRO> thirdPartLogin(UserThirdPartRO userThirdPart) {
        if (null == userThirdPart) {
            LOGGER.info("userServiceFacade.thirdPartLogin param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param can not be null");
        }
        if (null == userThirdPart.getBiz() || null == userThirdPart.getAppCode()) {
            LOGGER.info("userServiceFacade.thirdPartLogin param biz and appCode can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param biz and appCode can not be null");
        }
        if (null == userThirdPart.getLoginTypeEnum()) {
            LOGGER.info("userServiceFacade.thirdPartLogin param loginTypeEnum can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param loginTypeEnum can not be null");
        }
        if (null == userThirdPart.getOpenId()) {
            LOGGER.info("userServiceFacade.thirdPartLogin param openId can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param openId can not be null");
        }
        return userAccessApplication.thirdPartLogin(userThirdPart);
    }

    @Override
    public Response<String> bindUserMobileByOpenId(UserThirdPartRO userThirdPart) {
        if (null == userThirdPart) {
            LOGGER.info("userServiceFacade.bindUserMobileByOpenId param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param can not be null");
        }
        if (null == userThirdPart.getMobile()) {
            LOGGER.info("userServiceFacade.bindUserMobileByOpenId param mobile not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param mobile can not be null");
        }
        if (null == userThirdPart.getLoginTypeEnum()) {
            LOGGER.info("userServiceFacade.bindUserMobileByOpenId param loginTypeEnum can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param loginTypeEnum can not be null");
        }
        if (null == userThirdPart.getOpenId()) {
            LOGGER.info("userServiceFacade.bindUserMobileByOpenId param openId can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param openId can not be null");
        }
        return userAccessApplication.bindUserMobileByOpenId(userThirdPart);
    }

    @Override
    public Response<UserAccessRO> thirdSocialPartLogin(UserThirdPartRO userThirdPart) {
        if (null == userThirdPart) {
            LOGGER.info("userServiceFacade.thirdPartLogin param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param can not be null");
        }
        if (null == userThirdPart.getBiz() || null == userThirdPart.getAppCode()) {
            LOGGER.info("userServiceFacade.thirdPartLogin param biz and appCode can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param biz and appCode can not be null");
        }
        if (null == userThirdPart.getLoginTypeEnum()) {
            LOGGER.info("userServiceFacade.thirdPartLogin param loginTypeEnum can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param loginTypeEnum can not be null");
        }
        if (null == userThirdPart.getOpenId()) {
            LOGGER.info("userServiceFacade.thirdPartLogin param openId can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param openId can not be null");
        }
        return userAccessApplication.thirdSocialPartLogin(userThirdPart);
    }

    @Override
    public Response<UserAccessRO> bindUserMobileBySocialOpenId(UserThirdPartRO userThirdPart) {
        if (null == userThirdPart) {
            LOGGER.info("userServiceFacade.bindUserMobileByOpenId param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param can not be null");
        }
        if (null == userThirdPart.getMobile()) {
            LOGGER.info("userServiceFacade.bindUserMobileByOpenId param mobile not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param mobile can not be null");
        }
        if (null == userThirdPart.getLoginTypeEnum()) {
            LOGGER.info("userServiceFacade.bindUserMobileByOpenId param loginTypeEnum can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param loginTypeEnum can not be null");
        }
        if (null == userThirdPart.getOpenId()) {
            LOGGER.info("userServiceFacade.bindUserMobileByOpenId param openId can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userServiceFacade.thirdPartLogin param openId can not be null");
        }
        return userAccessApplication.bindUserMobileBySocialOpenId(userThirdPart);
    }
}
